﻿using eDataBoard.basePage;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System;
using OpcSelfHost.WebApi.model;
using Newtonsoft.Json;
using OPCDa;
using OpcSelfHost.basePage;
using OpcSelfHost.printer;
using System.Drawing;
using System.Globalization;

namespace OpcSelfHost.WebApi.controllers
{
    [RoutePrefix("api")]
    public class PrintController : ApiController
    {
        [HttpGet]
        [HttpPost]
        public ReturnMsg run(JObject jsonObj)
        {

            object return_data = null;
            string return_msg = null;
            JToken contents = null;
            try
            {
                string title = jsonObj.GetValue("title").ToString();
                contents = jsonObj.GetValue("contents");
                string[] msgs = contents.Select(s => s.Value<string>()).ToArray();

                BrPrinterData.Init.init();

                BrPrinterData.Init.addDataByte(BrPosCommand.t1b40());// 初始化
                BrPrinterData.Init.addDataByte(BrPosCommand.t1b61(0)); //居左
                Image img = Properties.Resources.logotianke;
                Bitmap bitmap = new Bitmap(img);
                BrPrinterData.Init.addDataByte(BrPosCommand.Print_1D76(bitmap)); //打印

                BrPrinterData.Init.addDataByte(BrPosCommand.t1b61(1)); //居中
                                                                       // BrPrinterData.Init.addDataByte(BrPosCommand.t1d21(0x02));//2倍高
                BrPrinterData.Init.addDataByte(BrPosCommand.t1d21(0x11));//1倍高倍宽
                BrPrinterData.Init.addDataByte("\n"+ title + "\n\n");
                BrPrinterData.Init.addDataByte(BrPosCommand.t1b61(0)); //居左
                                                                       // BrPrinterData.Init.addDataByte(BrPosCommand.t1d21(0x00));//默认大小
                                                                       // BrPrinterData.Init.addDataByte(BrPosCommand.t1d21(0x01));//2倍高
                BrPrinterData.Init.addDataByte(BrPosCommand.t1d21(0x11));//1倍高倍宽
                for (int i = 0; i < msgs.Length; i++)
                {
                    BrPrinterData.Init.addDataByte(msgs[i]+ "\n");
                } 
                BrPrinterData.Init.addDataByte("\n\n\n");

                // BrPrinterData.Init.addDataByte(BrPosCommand.t1d21(0x00));//恢复默认大小
                //  BrPrinterData.Init.addDataByte(BrPosCommand.t1b61(1)); //居中 

                //  BrPrinterData.Init.addDataByte(BrPosCommand.t1b61(2)); //居右


                // BrPrinterData.Init.addDataByte("打印一维码\n");
                //  BrPrinterData.Init.addDataByte(BrPosCommand.t1d6b(67, "123456789012")); 

                //BrPrinterData.Init.addDataByte(BrPosCommand.t1b61(1)); //居中 
                BrPrinterData.Init.addDataByte(BrPosCommand.PrintAndFeedPaper(120));//走纸
                BrPrinterData.Init.addDataByte(BrPosCommand.Cutter());//切纸指令

                SocketClient.Instance.WriteFile(BrPrinterData.Init.getDataByte());//一次性发送整包数据
                return ReturnMsg.setReturnMsg(0, "打印成功");

            }
            catch (Exception ex)
            {
                return_msg = ex.Message;
                return ReturnMsg.setReturnMsg(1, return_msg, return_data);
            }




        }

    }
}
